Test Series - java script

Test Number 3/92

Q: Which of the following is not considered as an error in JavaScript?
A. Syntax error
B. Missing of semicolons
C. Division by zero
D. Missing of Bracket
Solution: Division by zero is not an error in JavaScript: it simply returns infinity or negative infinity. There is one exception, however: zero divided by zero does not have a well defined value, and the result of this operation is the special not-a-number value, printed as NaN.
Q: The escape sequence ‘f’ stands for _________
A. Floating numbers
B. Representation of functions that returns a value
C. f is not present in JavaScript
D. Form feed
Solution: f is the JavaScript escape sequence that stands for Form feed (u000C). It skips to the start of the next page. (Applies mostly to terminals where the output device is a printer rather than a VDU).
Q: The snippet that has to be used to check if “a” is not equal to “null” is _________
A. if(a!=null)
B. if (!a)
C. if(a!null)
D. if(a!==null)
Solution: A strict comparison (e.g., ===) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. ==) converts the operands to the same type before making the comparison. The not-equal operator !== compares 0 to null and evaluates to either true or false.
Q: The statement a===b refers to _________
A. Both a and b are equal in value, type and reference address
B. Both a and b are equal in value
C. Both a and b are equal in value and type
D. There is no such statement
Solution: ”===” operator is known as the strict comparison operator. A strict comparison (===) is only true if the operands are of the same type and the contents match.
Q: Assume that we have to convert “false” that is a non-string to string. The command that we use is (without invoking the “new” operator).
A. false.toString()
B. String(false)
C. String newvariable=”false”
D. Both false.toString() and String(false)
Solution: The three approaches for converting to string are: value.toString(),”” + value and String(value). A non-string can be converted in two ways without using a new operator false.toString () and String(false).
Q: What will be the output of the following JavaScript code?

function compare()
{
    int num=2;
    char b=2;
    if(a==b)
        return true;
    else
        return false;
}
A. true
B. false
C. runtime error
D. compilation error
Solution: The == convert different type of operands to the same type before making the comparison. A strict comparison results into true value if the operands are of the same type and the contents match.
Q: What will be the output of the following JavaScript code?

function equalto()
{
    int num=10;
    if(num===”10”)
        return true;
    else
        return false;
}
A. true
B. false
C. runtime error
D. compilation error
Solution: A === operator is only true if the operands are of the same type and the contents match. Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Q: What will be the output of the following JavaScript code?

function compare()
{
    int a=1;
    char b=1;
    if(a.tostring()===b)
        return true;
    else 
        return false;
}
A. true
B. false
C. runtime error
D. logical error
Solution: A non string (integer) can be converted to string using .tostring() function. A strict comparison is only true if the operands are of the same type and the contents match. Hence the following code snippet would result into true output.
Q: What will be the output of the following JavaScript code?

int a==2;
int b=4;
int ans=a+b;
print(ans);
A. 2
B. 6
C. 0
D. error
Solution: The following code will generate into an error output as a comparator operator is used outside of if statement. A single equalto (’=’) operator is used for initialization.
Q: What will be the output of the following JavaScript code?

int a=1;
if(a!=null)
    return 1;
else
    return 0;
A. 1
B. 0
C. runtime error
D. compiler error
Solution: != is the not equal to operator. It gives a value of 1 if the two values which are compared are not equal and give 0 if the two values are equal.

You Have Score    /10